package com.hison.tandainbao.service.impl;

import com.alibaba.druid.sql.visitor.functions.Now;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.hison.tandainbao.entiy.pojo.domain.TbFeedback;
import com.hison.tandainbao.entiy.pojo.domain.User;
import com.hison.tandainbao.mapper.UserMapper;
import com.hison.tandainbao.response.ResponseResult;
import com.hison.tandainbao.service.TbFeedbackService;
import com.hison.tandainbao.mapper.TbFeedbackMapper;
import com.hison.tandainbao.util.UserHolder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;

/**
* @author hison
* @description 针对表【tb_feedback】的数据库操作Service实现
* @createDate 2024-03-30 21:54:58
*/
@Service
public class TbFeedbackServiceImpl extends ServiceImpl<TbFeedbackMapper, TbFeedback>
    implements TbFeedbackService{

    @Autowired
    private TbFeedbackMapper mapper;

    @Override
    public ResponseResult addFeedback(TbFeedback tbFeedback) {
        //Long id = UserHolder.getUser().getId();
        tbFeedback.setUserId(1L);
        LocalDateTime now = LocalDateTime.now();
        // 将LocalDateTime转换为Date
        Date date = Date.from(now.atZone(java.time.ZoneId.systemDefault()).toInstant());
        tbFeedback.setSubmitDate(date);



        int i = mapper.insert(tbFeedback);
        if (i< 1){
            return  ResponseResult.error("添加失败");
        }
        return ResponseResult.ok("添加成功");
    }

    @Override
    public ResponseResult getMyFeedback() {
//        Long id = UserHolder.getUser().getId();
        List<TbFeedback> list = mapper.getMyFeedback(1L);
        if (list.size() <= 0  ) {
            return  ResponseResult.error("您还未发布反馈信息");
        }
        return ResponseResult.ok(list);
    }

    @Override
    public ResponseResult deleteFeedBack(Integer feedBackId) {
        Long id = UserHolder.getUser().getId();
        QueryWrapper<TbFeedback> wrapper = new QueryWrapper<>();
        wrapper.eq("id", feedBackId);
        wrapper.eq("user_id", id);
        int delete = mapper.delete(wrapper);
        if (delete  < 1) {
            return  ResponseResult.error("删除失败");
        }
        return ResponseResult.ok("删除成功");
    }

    @Override
    public ResponseResult getAllFeedBack(Integer current,  Integer total) {
        PageHelper.startPage(current, total);
        List<TbFeedback> list = query().orderByAsc("submit_date").list();
        //获取用户姓名
        list.forEach(item ->{
            item.setUserName(getUserName(item.getUserId()));
        });
        if (list.size() <= 0){
            return  ResponseResult.error("还没有用户反馈信息");
        }
        return ResponseResult.ok(list);
    }

    @Autowired
    private UserMapper userMapper;
    private String getUserName(Long userId) {
        User user = userMapper.selectById(userId);
        return user == null ? "" : user.getName();
    }
}




